package cn.iocoder.yudao.module.squaredancing.controller.app.download;

import cn.iocoder.yudao.module.squaredancing.controller.app.download.vo.DownloadPageReqVO;
import cn.iocoder.yudao.module.squaredancing.controller.app.download.vo.DownloadRespVO;
import cn.iocoder.yudao.module.squaredancing.controller.app.download.vo.DownloadSaveReqVO;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.squaredancing.dal.dataobject.download.DownloadDO;
import cn.iocoder.yudao.module.squaredancing.service.download.DownloadService;

@Tag(name = "管理后台 - 视频下载")
@RestController
@RequestMapping("/squaredancing/download")
@Validated
public class DownloadController {

    @Resource
    private DownloadService downloadService;

    @PermitAll
    @PostMapping("/create")
    @Operation(summary = "创建视频下载")
    public CommonResult<Long> createDownload(@Valid @RequestBody DownloadSaveReqVO createReqVO) {
        return success(downloadService.createDownload(createReqVO));
    }

    @PermitAll
    @PutMapping("/update")
    @Operation(summary = "更新视频下载")
    public CommonResult<Boolean> updateDownload(@Valid @RequestBody DownloadSaveReqVO updateReqVO) {
        downloadService.updateDownload(updateReqVO);
        return success(true);
    }

    @PermitAll
    @DeleteMapping("/delete")
    @Operation(summary = "删除视频下载")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDownload(@RequestParam("id") Long id) {
        downloadService.deleteDownload(id);
        return success(true);
    }

    @PermitAll
    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除视频下载")
    public CommonResult<Boolean> deleteDownloadList(@RequestParam("ids") List<Long> ids) {
        downloadService.deleteDownloadListByIds(ids);
        return success(true);
    }

    @PermitAll
    @GetMapping("/get")
    @Operation(summary = "获得视频下载")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DownloadRespVO> getDownload(@RequestParam("id") Long id) {
        DownloadDO download = downloadService.getDownload(id);
        return success(BeanUtils.toBean(download, DownloadRespVO.class));
    }

    @PermitAll
    @GetMapping("/page")
    @Operation(summary = "获得视频下载分页")
    public CommonResult<PageResult<DownloadRespVO>> getDownloadPage(@Valid DownloadPageReqVO pageReqVO) {
        PageResult<DownloadDO> pageResult = downloadService.getDownloadPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DownloadRespVO.class));
    }

    @PermitAll
    @GetMapping("/export-excel")
    @Operation(summary = "导出视频下载 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDownloadExcel(@Valid DownloadPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DownloadDO> list = downloadService.getDownloadPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频下载.xls", "数据", DownloadRespVO.class,
                        BeanUtils.toBean(list, DownloadRespVO.class));
    }

}